package edu.hnu.userserver.controller;

import edu.hnu.commonutils.Result;
import edu.hnu.commonutils.UserDb.TradersResponse;
import edu.hnu.commonutils.model.User;
import edu.hnu.userserver.auth.JwtOperator;
import edu.hnu.userserver.service.TradersService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
//@CrossOrigin(origins = "*")
@RequestMapping("/trader")
@Api(value = "Trader接口",tags = {"Trader"})
public class TradersController {

    @Autowired
    TradersService tradersService;

    @Autowired
    JwtOperator jwtOperator;
    private User parseToken(String token){
        Claims claims = jwtOperator.getClaimsFromToken(token);
        User user = new User(-1L,-2,false);
        try {
            Long id = Long.parseLong(claims.get("id").toString());
            int userType= Integer.parseInt(claims.get("user_type").toString());
            if (userType != 0) {
                // 判断是否是不是trader
                user.setAuthorized(false);
            }else {
                user.setUserType(userType);
                user.setId(id);
                user.setAuthorized(true);
            }
        } catch (NumberFormatException e) {
            user.setAuthorized(false);
            return user;
        }
        return user;
    }

    @GetMapping("/login")
    @ApiOperation(value = "交易员trader登录",notes = "输入用户名(邮箱) 密码，trader登录")
    public Result TraderLogin(@ApiParam(value = "用户名或者邮箱") @RequestParam String usernameOrEmail,@ApiParam(value = "密码") @RequestParam  String password){
        System.out.println("这是TraderLogin controller");
        return  tradersService.TraderLogin(usernameOrEmail,password);
    }

    @GetMapping("/getTraderDetail")
    @ApiOperation(value = "获取单个trader的信息",notes = "输入traderid 获取名字和邮箱")
    public TradersResponse getTraderDetail(@ApiParam(value = "traderId 唯一标识") @RequestParam Integer id){
         return tradersService.getTraderDetail(id);
    }
}
